////////////////////////////////////////////////////////////////////////////////
//////// PARLIAMENTARY ELECTIONS - EXECUTIVE TURNOVERS /////////////////////////
////////////////////////////////////////////////////////////////////////////////

// This do-file associates each parliamentary election with a running variable and treatment variable for electoral turnovers. 

////////////////////////////////////////////////////////////////////////////////
//////// a. Defining an incumbent party for each election
////////////////////////////////////////////////////////////////////////////////

// For turnovers in the executive branch, we define the party in power before/after the election as the leading party during a period of at least 365 days during the two years preceding/following the election.

// We import the parliamentary election database and keep only variables of interest
use "$project_path/data/3_cleaned/parliamentary_elections", clear

keep Country Year Month Type_Election Date Source Total_Seats Party_* Seats_* Seat_Share_* flag*

// We drop elections for which we have no data on results
drop if Source==""
drop if Seat_Share_1==. | Seat_Share_1==0
// We drop purely constituent elections
drop if flag_constituent==1
// We drop elections with appointed members
drop if flag_appointed==1
// We drop inconsequential elections
drop if flag_inconsequential==1

// We merge with the characteristics of elections and check that an elected leader is always defined
merge 1:1 Country Year Month Type_Election using "$project_path/data/2_intermediary/leaders/elected_leaders", keep(match) assert(using match) nogen 

// In this specification, we only focus on elections that led to the nomination of a HOS or HOG
assert elected_leader=="HOS" | elected_leader=="HOG" | elected_leader=="No leader" | elected_leader=="Indeterminate"
drop if elected_leader=="No leader" | elected_leader=="Indeterminate"

tempfile parl_results
save `parl_results'

****	1.	Automatic assignment

import excel "$project_path/data/2_intermediary/leaders/leaders_before_after.xlsx", firstrow clear
destring Month, replace
merge 1:1 Country Year Month Type_Election using `parl_results', keep(match) assert(master match) nogen 

gen incumbent_leader = ""
gen incumbent_party = ""
gen leader_after = ""
gen party_after = ""
foreach leader_type in HOS HOG {
	replace incumbent_leader = incumbent_leader_`leader_type' if elected_leader=="`leader_type'"
	replace incumbent_party = incumbent_party_`leader_type' if elected_leader=="`leader_type'"
	replace leader_after = leader_after_`leader_type' if elected_leader=="`leader_type'"
	replace party_after = party_after_`leader_type' if elected_leader=="`leader_type'"
}
drop incumbent_leader_HOS incumbent_party_HOS leader_after_HOS party_after_HOS incumbent_leader_HOG incumbent_party_HOG leader_after_HOG party_after_HOG
format elected_leader Type_Election %10s
format Country incumbent_leader incumbent_party leader_after party_after %20s

****	2.	Manual definition

// In some cases, we fail to define the incumbent parties before/after automatically. In these cases, we attempt to define manually the "incumbent_party" and/or the "party_after". Such manual definitions are stored in Excel spreadsheets.

// We import manually coded incumbents
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/manual_incumbent_party.xlsx", firstrow clear 
drop comment incumbent_party_ID
merge 1:1 Country Year Month using `temp', assert(using match) nogen  

// We include manually coded data in our dataset
replace incumbent_party = manual_incumbent_party if manual_incumbent_party!=""
drop manual_incumbent_party

// We import manually coded incumbent parties after
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/manual_party_after.xlsx", firstrow clear 
drop comment incumbent_party_ID
merge 1:1 Country Year Month using `temp', assert(using match) nogen

// We include manually coded data in our dataset
replace party_after = manual_party_after if manual_party_after!=""
drop manual_party_after

tempfile alternative_specification_elec
save `alternative_specification_elec'

////////////////////////////////////////////////////////////////////////////////
//////// b. Matching incumbent parties with parties in the election
////////////////////////////////////////////////////////////////////////////////

/*
We then attempt to match the party defined as the incumbent party with one of the parties in the election results, which will be defined as the party representing the  incumbency. The party representing the incumbency is the incumbent party if it took part in the election, the party which succeded the incumbent party in case of a name change or merger, or the party which was supported by the incumbent party if it did not take part in the election itself.

In most cases, the representative of the incumbency can be selected automatically: a Python script compares the incumbent party with the list of parties competing in the election, and tries to find a match. If the match is fuzzy, it is checked manually. If no match could be performed automatically, we try to manually define a representative of the incumbency.
*/

****	1.	Automatic matching

// We drop cases for which we will not be able to define a running variable (we explain why in the excel spreadsheet we are importing)
import excel using "$project_path/data/1_input/manual_matches/parliamentary_alt/no_run_and_treatment_var.xlsx", firstrow clear
merge 1:1 Country Year Month Type_Election elected_leader using `alternative_specification_elec', assert(using match) nogen 
drop if no_runvar==1

// We drop independent incumbent leaders as it is impossible to match one party from the election results with them
drop if inlist(incumbent_party, "independent", "Independent", "independent politician", "Independent politician", "non-attached", "nonpartisanism")

// We performed a fuzzy match. Its results are stored in an Excel spreadsheet.
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/fuzzy_match_runvar.xlsx", firstrow clear
keep Country Year Month party_matched
merge 1:1 Country Year Month using `temp', assert(match) nogen
ren party_matched party_incumbency

****	2.	Manual matching

// When the incumbent party did run in the election but the automatic matching algorithm failed to make a link, we perform a manual match, documented in an Excel spreadsheet. We also include in this spreadsheet cases where the fuzzy match was incorrect and was corrected manually.
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/manual_match_runvar.xlsx", firstrow clear
merge 1:1 Country Year Month using `temp', assert(using match) nogen

// Consistency check
assert incumbent_party_to_match==incumbent_party if party_matched_manual!=""

// Including manual changes in the database
replace party_incumbency = party_matched_manual if party_matched_manual!=""
drop incumbent_party_to_match party_matched_manual comment

****	3.	Manual representative of the incumbency

// Sometimes, the representative of the incumbency is not the incumbent party, for example because the incumbent party changed its name, or because the incumbent party supported another party. For these cases, we define manually a representative of the incumbency.

// We import manually coded data and merge it to our dataset
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/manual_party_incumbency.xlsx", firstrow clear
merge 1:1 Country Year Month using `temp', assert(using match) nogen 

// We include manually coded data in our dataset
replace party_incumbency = manual_party_incumbency if manual_party_incumbency!=""
drop manual_party_incumbency comment

////////////////////////////////////////////////////////////////////////////////
//////// c. Defining the running variable
////////////////////////////////////////////////////////////////////////////////

// After defining the representative of the incumbency, we find the representative of the opposition, and define the running variable as the difference in their vote shares. 

****	1.	Special case: independents arriving 1st or 2nd

// When independents get a large share of the vote, we manually define the representative of the incumbency and the representative of the opposition.

tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/nonpartisan.xlsx", firstrow clear
merge 1:1 Country Year Month using `temp', assert(using match) nogen

replace party_incumbency = manual_incumbent_party if manual_incumbent_party!=""
ren manual_opposition_party party_opposition 
drop comment manual_incumbent_party

****	2.	Baseline assignment

// We find the rank of the representative of the incumbency and the opposition (when manually defined)
gen rank_incumbency=.
gen rank_opposition=.
forvalues i = 1/74 {
	replace rank_incumbency = `i' if party_incumbency==Party_`i' & party_incumbency!="" 
	replace rank_opposition = `i' if party_opposition==Party_`i' & party_opposition!="" 
}
assert rank_incumbency!=. if party_incumbency!=""
assert rank_opposition!=. if party_opposition!=""

// Define incumbent and opposition shares
gen double share_incumbent = .
gen double share_opposition = .
forvalues i=1/74 {
	replace share_incumbent = Seat_Share_`i' if rank_incumbency==`i'
	replace share_opposition = Seat_Share_`i' if rank_opposition==`i'
}

// When the rank of the opposition has not been manually defined, we define it as the rank of the best-ranked party (excluding the representative of the incumbency) and set the seat share accordingly. 
replace share_opposition = Seat_Share_1 if rank_incumbency>1 & rank_incumbency!=. & rank_opposition==.
replace party_opposition = Party_1 if rank_incumbency>1 & rank_incumbency!=. & rank_opposition==.
replace share_opposition = Seat_Share_2 if rank_incumbency==1 & rank_opposition==.
replace party_opposition = Party_2 if rank_incumbency==1 & rank_opposition==.
replace rank_opposition = 1 if rank_incumbency>1 & rank_incumbency!=. & rank_opposition==.
replace rank_opposition = 2 if rank_incumbency==1 & rank_opposition==.

****	3.	Special case: the incumbent party won no seats although it was active at the moment of the election (the incumbent party is not in the list of results. This may occur for example when the source we use only documents parties which won seats at the election). In this case, we set the share of the incumbent to 0, and the share of the opposition to the share of the best ranked party. The list of these cases is documented in an Excel spreadsheet.

// We import manually coded data and merge it to our dataset
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/no_seats.xlsx", firstrow clear
gen incumbent_noseat = 1
merge 1:1 Country Year Month using `temp', assert(using match) nogen

// We include manually coded data in our dataset
replace share_incumbent = 0.0 if incumbent_noseat==1
replace share_opposition = Seat_Share_1 if incumbent_noseat==1
replace rank_opposition = 1 if incumbent_noseat==1
replace party_incumbency = "" if incumbent_noseat==1
drop incumbent_noseat comment

****	4.	Special case: the incumbent party won all seats. If the incumbent is the only party competing in the election, we set the running variable to 100.

replace share_incumbent = 100.0 if rank_incumbency==1 & (Party_2=="" | Seat_Share_2==0)
replace share_opposition = 0.0 if rank_incumbency==1 & (Party_2=="" | Seat_Share_2==0)

****	5.	"Independents" parties

// Although the total number of independents is reported in the results, we consider each independent as belonging to a seperate "party" having one seat in parliament. Thus, the only case in which independents are the opposition party is when there is only the party of the incumbency and independents in parliament.

assert Party_3=="" if party_opposition=="Independents"
assert Party_3=="" if party_opposition=="Others"

// Cases where there are only two parties in parliament: the incumbent party and independents (or "others")
gen temp_case = 1 if party_opposition=="Independents" & Party_2=="Independents" & Party_3==""
replace temp_case = 1 if party_opposition=="Others" & Party_2=="Others" & Party_3==""
replace share_opposition =  Seat_Share_2/Seats_2 if temp_case==1 & Seats_2!=0
replace temp_case = 2 if party_opposition=="Other parties" & Party_1=="Other parties" & Party_3==""
replace share_opposition =  Seat_Share_1/Seats_1 if temp_case==2 & Seats_1!=0
drop temp_case

****	6.	Definition of the running variable and checks

sort Country Year Month Type_Election
gen double runvar = share_opposition - share_incumbent

// When aggregating the seat shares of different parties in coalition, there are rounding errors (at the 15th decimal point) despite storing variables as a double. To make sure we correctly exclude elections with a runvar equal to 0, we perform the following adjustment:
replace runvar = round(runvar, 1e-12)
replace runvar = 0 if abs(runvar)<0.00000000001 
count if abs(runvar)<0.01 & runvar!=0
assert `r(N)'==0

// We check that we documented all cases in which no running variable could be defined.
assert runvar!=.

keep Country Year Month Type_Election incumbent_leader incumbent_party leader_after party_after party_incumbency rank_incumbency share_incumbent party_opposition rank_opposition share_opposition runvar flag_*
format incumbent_leader incumbent_party leader_after party_after party_opposition party_incumbency %20s
tempfile runvar
save `runvar'

////////////////////////////////////////////////////////////////////////////////
//////// d. Defining the treatment
////////////////////////////////////////////////////////////////////////////////

/* Unlike in the baseline specification, we now define a treatment variable independently from the assignment variable. Indeed, executive turnovers are defined from the incumbent leader/party and the leading leader/party after the election. More precisely, we now set the treatment variable to 0 if:
	a.	The incumbent leader and the leader after the election are the same person;
	b.	Else, if the incumbent leading party and the leading party after the election are the same;
	c.	Else, if the incumbent leading party did not compete in the election and supported the leading party after the election party during the election.
Rules a. & b. are implemented automically. Rule c. is implemented manually.
*/

****	1.	Automatic matching

// We drop cases for which we will not be able to define a treatment variable (we explain why in the excel spreadsheet we are importing)
import excel using "$project_path/data/1_input/manual_matches/parliamentary_alt/no_run_and_treatment_var.xlsx", firstrow clear
merge 1:1 Country Year Month Type_Election elected_leader using `alternative_specification_elec', assert(using match) nogen     
drop if no_treatment==1

// We do not consider cases in which the incumbent leader or the leader after the election were independents, as it is often difficult to know if these leaders had the same leaning or not. We could assign the treatment to 0 when the leader before the election and the leader after the election is the same person, but this would lead us to disproportionately code the treatment variable on one side of the threshold.
drop if inlist(lower(incumbent_party), "independent", "independent politician", "non-attached", "nonpartisanism")
drop if inlist(lower(party_after), "independent", "independent politician", "non-attached", "nonpartisanism")

// We perform a fuzzy match
tempfile temp
save `temp'
import excel "$project_path/data/1_input/manual_matches/parliamentary_alt/fuzzy_match_treatment.xlsx", firstrow clear
destring treatment, replace
merge 1:1 Country Year Month incumbent_leader incumbent_party using `temp', assert(match) nogen

****	2.	Manual adjustments to the treatment variable

tempfile temp
save `temp'
import excel using "$project_path/data/1_input/manual_matches/parliamentary_alt/manual_match_treatment.xlsx", firstrow clear
merge 1:1 Country Year Month using `temp', assert(using match) nogen
replace treatment = manual_treatment if manual_treatment!=.
drop elected_leader comment manual_treatment

keep Country Year Month Type_Election treatment
merge 1:1 Country Year Month using `runvar', nogen 

////////////////////////////////////////////////////////////////////////////////
//////// e. Wikidata ID of the leader before the election and the candidate of the incumbency
////////////////////////////////////////////////////////////////////////////////

// We find the Wikidata ID of the leader before the election and of the candidate of the incumbency (only for elections in parliamentary regimes).

*** Leader before the election
tempfile temp
save `temp'
import excel "$project_path/data/1_input/other/leaders/party_affiliations.xlsx", firstrow clear
keep Country Leader wikidata_id
ren Leader incumbent_leader
ren wikidata_id incumbent_leader_wid
format incumbent_leader %20s
merge 1:m Country incumbent_leader using `temp', keep(using match)
assert incumbent_leader=="" if _merge==2
drop _merge

***	Candidate of the incumbency
tempfile temp
save `temp'
use "$project_path/data/1_input/elections/Wikipedia_links/links_parl.dta", clear
keep Country Year Month Party leader_wikidata_id reg_type
ren Party party_incumbency 
ren leader_wikidata_id candidate_incumbency_wid
merge 1:1 Country Year Month party_incumbency using `temp', keep(using match)
assert (flag_not_last==1 | party_incumbency=="") if _merge==2
drop _merge

////////////////////////////////////////////////////////////////////////////////
///////// Exporting results
////////////////////////////////////////////////////////////////////////////////

ren runvar runvar_alt
ren treatment treatment_alt

local vlist = "Country Year Month Type_Election runvar_alt treatment_alt incumbent_party party_incumbency rank_incumbency share_incumbent party_opposition rank_opposition share_opposition incumbent_leader incumbent_leader_wid candidate_incumbency_wid flag*"
keep `vlist'
order `vlist'
sort Type_Election Country Year Month

lab var rank_incumbency				 	"Rank of the incumbency"
lab var incumbent_party					"Incumbent party"
lab var party_incumbency				"Party representing the incumbency"
lab var party_opposition				"Party representing the opposition"
lab var rank_opposition					"Rank of the opposition"
lab var share_incumbent				 	"Vote share of the candidate representing the incumbency"
lab var share_opposition				"Vote share of the candidate representing the opposition"
lab var runvar_alt						"Running variable (executive turnovers)"
lab var treatment_alt					"Treatment (executive turnovers)"
lab var incumbent_leader 				"Incumbent leader"
lab var incumbent_leader_wid 			"Incumbent leader Wikidata ID"
lab var candidate_incumbency_wid		"Candidate of the incumbency Wikidata ID"

compress
format incumbent_party party_incumbency party_opposition incumbent_leader %20s
format incumbent_leader_wid candidate_incumbency_wid %15s
notes drop _dta
save "$project_path/data/3_cleaned/turnovers_parliamentary_executive", replace

